""" 英雄机的类
基本的调用函数
set_pos(self,x,y) 设置飞机初始位置

press_move(self) 持续移动

"""
from setting import *
from lifebar import *
from herobullet import *


class HeroPlane(pygame.sprite.Sprite):
    def __init__(self, hero_index):
        self._layer = 1
        self.groups = allgroup, herogroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        # 默认图像及尺寸
        self.image = pygame.image.load('images/hero/hero' + str(hero_index) +
                                       '.png').convert_alpha()
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        #动起来的速度，设置成零的话，以后就动不了了。
        if hero_index == 0:
            self.speed = 4
            self.damage = 4
            self.HP = 100
        elif hero_index == 1:
            self.speed = 3
            self.damage = 5
            self.HP = 100
        elif hero_index == 2:
            self.speed = 5
            self.damage = 3
            self.HP = 100
        elif hero_index == 3:
            self.speed = 5
            self.damage = 5
            self.HP = 50
        self.bullet_speed = -6
        self.HPFULL = self.HP
        #飞机的x,y初始速度，不动。
        self.x_speed = 0
        self.y_speed = 0
        #子弹类型
        self.bullet_type = 0
        #发射类型
        self.fire_type = 0
        #发射按键储存
        self.fire_key_list = []
        #设置导弹发射间隔 默认 1000毫秒
        self.bullet_start_time= pygame.time.get_ticks()
        self.bullet_interval = 400

        # 移动按键储存
        self.key_down_list = []

        lifebar = Lifebar(self)

    #计算飞机位置，精灵组会自动调用
    def update(self):
        """ 
        更新飞机位置
            飞机位置超出 scene时，不进行运算
        """
        #计算飞机的位置
        self.rect.x += self.x_speed
        self.rect.y += self.y_speed
        #限制飞机在场景scene
        if self.rect.x <= 0:
            self.rect.x = 0
        elif self.rect.x > SCENEWIDTH - self.rect.width:
            self.rect.x = SCENEWIDTH - self.rect.width
        elif self.rect.y <= 0:
            self.rect.y = 0
        elif self.rect.y > SCENEHEIGHT - self.rect.height:
            self.rect.y = SCENEHEIGHT - self.rect.height

        if self.HP <= 0:
            self.kill()

    def set_speed(self, speed=4):
        self.speed = speed

    #设置飞机位置，场景初始化时用
    def set_pos(self, x, y):
        """ 
        设置飞机位置
            x 左上角x点坐标
            y 左上角y点坐标
        """
        self.rect.x = x
        self.rect.y = y

    #计算飞机的位置，挨揍用
    def get_pos(self):
        """ 
        返回 vector2的值
            example:
            pos1 = get_pos()
            print(pos1.x ,pos1.y)
        """
        return vect(self.rect.x, self.rect.y)

    #飞机移动，把移动的记录存到列表里，在场景的按键里判断，做出反应
    def move(self, direction):
        """ 
        移动飞机
            direction 方向，可以为string值
            RIGHT
            LEFT
            UP
            DOWN
            example：  move("RIGHT")
        """
        #先把字符串全转换为大写
        # x,y方向上不动的，必须设置成 0
        direction = direction.upper()
        if direction == 'RIGHT':
            self.x_speed = self.speed
            self.y_speed = 0
        elif direction == 'LEFT':
            self.x_speed = -self.speed
            self.y_speed = 0
        elif direction == "UP":
            self.x_speed = 0
            self.y_speed = -self.speed
        elif direction == 'DOWN':
            self.x_speed = 0
            self.y_speed = self.speed
        else:
            pass

    #移动按键按下，存到list里
    def key_down(self, key):
        self.key_down_list.append(key)

    #移动按键松开，清空列表，初始化速度
    def key_up(self, key):
        if len(self.key_down_list) != 0:
            try:
                self.key_down_list.remove(key)
                #速度再次初始,speed不能初始，不然动不了了。
                # self.speed  = 0
                self.x_speed = 0
                self.y_speed = 0
            except Exception:
                pass
    # 发射键按下向列表添加 j
    def fire_key_down(self,key):
        self.fire_key_list.append(key)
    # 发射键松开向列表删除 j
    def fire_key_up(self, key):
        if len(self.fire_key_list) != 0:  # 判断是否为空
            try:
                self.fire_key_list.remove(key)
            except Exception:
                raise
    #持续移动
    #如果移动键列表不为零，则根据列表数据，相应移动
    def press_move(self):
        if len(self.key_down_list) != 0:
            if self.key_down_list[0] == pygame.K_a:
                self.move('LEFT')
            elif self.key_down_list[0] == pygame.K_d:
                self.move('RIGHT')
            elif self.key_down_list[0] == pygame.K_w:
                self.move('UP')
            elif self.key_down_list[0] == pygame.K_s:
                self.move("DOWN")

    def set_fire_type(self, firetype):
        self.fire_type = firetype

    def set_bullet_type(self, bullettye):
        self.bullet_type = bullettye

    def fire_bullet(self):

        firetype = self.fire_type
        bullettype = self.bullet_type
        #hero 子弹的发射位置当然是头部中心
        hero_fire_pos = vect(self.rect.midtop[0], self.rect.midtop[1])
        #单颗子弹
        if firetype == 0:
            bullet = HeroBullet()
            bullet.set_bullet_type(bullettype)
            pos_x = hero_fire_pos.x - bullet.rect.width // 2
            pos_y = hero_fire_pos.y-50
            bullet.set_speed(self.bullet_speed, angle=90)
            bullet.set_pos(pos_x, pos_y)
        # 两个
        elif firetype == 1:
            interval = -self.rect.width // 4
            for v in range(0, 2):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x - bullet.rect.width // 2 + interval
                pos_y = hero_fire_pos.y-50
                bullet.set_speed(self.bullet_speed, angle=90)
                bullet.set_pos(pos_x, pos_y)
                interval = self.rect.width // 4
        #三个
        elif firetype == 2:
            interval = -self.rect.width // 4
            for v in range(0, 3):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x - bullet.rect.width // 2 + interval
                pos_y = hero_fire_pos.y-50
                bullet.set_speed(self.bullet_speed, angle=90)
                bullet.set_pos(pos_x, pos_y)
                interval += self.rect.width // 4
        # 向下30度角
        elif firetype == 3:
            for angle in range(75, 106, 10):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x
                pos_y = hero_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #向下 60度角
        elif firetype == 4:
            for angle in range(60, 121, 10):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x
                pos_y = hero_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #向下 90度角
        elif firetype == 5:
            for angle in range(45, 136, 10):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x
                pos_y = hero_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #向下 180度
        elif firetype == 6:
            for angle in range(0, 181, 10):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x
                pos_y = hero_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #360度
        elif firetype == 7:
            for angle in range(0, 360, 10):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x
                pos_y = hero_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #超限了，还是360吧
        else:
            for angle in range(0, 360, 10):
                bullet = HeroBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = hero_fire_pos.x
                pos_y = hero_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
    # 按键j不放,持续开火
    def press_fire(self):
        """持续发射子弹
        """
        current_time = pygame.time.get_ticks()
        pass_time = current_time - self.bullet_start_time
        if len(self.fire_key_list) != 0 and pass_time > self.bullet_interval:
            self.fire_bullet()
            self.bullet_start_time = current_time

